package org.springcloud.wisdom.spring_02_classroom.student.choose.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springcloud.wisdom.spring_02_classroom.admin.createcourse.entity.Course;
import org.springcloud.wisdom.spring_02_classroom.student.choose.entity.ChooseCourse;

import java.util.List;

@Mapper
public interface ChooseCourseDao {
    //查看所有选课
    @Select("<script>" +
            "SELECT c.*, t.tname " +
            "FROM course c " +
            "JOIN teacher t ON c.tid = t.tid " +
            "WHERE iscanlook = 1 AND isdel = 0 " +
            "LIMIT #{start}, #{size} " +
            "</script>")
    public List<Course> get(int start, int size);

    @Select("<script>" +
            "SELECT count(*) FROM course " +
            "WHERE iscanlook = 1 AND isdel = 0" +
            "</script>")
    public int CountCourses();

    //选课
    @Insert("insert into choose(id,sid,cid,tid,ltime)" +
            "VALUES (#{id},#{sid},#{cid},#{tid},#{ltime})")
    public int insert(ChooseCourse chooseCourse);

    // 检查是否已经选了同一时间的课程
    @Select("SELECT COUNT(*) FROM choose c JOIN course co ON c.cid = co.cid WHERE c.sid = #{sid} AND co.ltime = #{ltime} AND c.isdel = 0")
    boolean isExistSameTimeCourse(String sid, String ltime);

    // 检查是否已经选过这门课程
    @Select("SELECT COUNT(*) FROM choose WHERE sid = #{sid} AND cid = #{cid} AND isdel = 0")
    boolean isAlreadyChosen(String sid, String cid);
}
